using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SearchForDestruction
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Map map;
        LinkedList<int> path;
        int sx, sy, ex, ey;
       
        public Game1()
        {
            this.IsMouseVisible = true;
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            Content.RootDirectory = "Content";
            map = new Map(50, 37);
            path = new LinkedList<int>();
            sx = sy = ex = ey = 5;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            
            map.Load(Content, "blhalahb.png");

            Util.Debug.Shape.LoadContent(GraphicsDevice, Content);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            
            MouseState state = Mouse.GetState();

            if (state.LeftButton == ButtonState.Pressed)
            {
                int x = state.X / 16;
                int y = state.Y / 16;
                if (x >= 0 && x < 50 && y >= 0 && y < 37)
                {
                    sx = x;
                    sy = y;
                    path = map.GetPath(sx, sy, ex, ey, Map.GROUND);
                }
            }
            if (state.RightButton == ButtonState.Pressed)
            {
                int x = state.X / 16;
                int y = state.Y / 16;
                if (x >= 0 && x < 50 && y >= 0 && y < 37)
                {
                    ex = x;
                    ey = y;
                    path = map.GetPath(sx, sy, ex, ey, Map.GROUND);
                }
            }

            KeyboardState keyboard = Keyboard.GetState();
            if (keyboard.IsKeyDown(Keys.D1))
            {
                int x = state.X / 16;
                int y = state.Y / 16;
                if (x >= 0 && x < 50 && y >= 0 && y < 37)
                {
                    map.SetTile(x, y, Map.GROUND, 1f);
                    path = map.GetPath(sx, sy, ex, ey, Map.GROUND);
                }
            }
            if (keyboard.IsKeyDown(Keys.D2))
            {
                int x = state.X / 16;
                int y = state.Y / 16;
                if (x >= 0 && x < 50 && y >= 0 && y < 37)
                {
                    map.SetTile(x, y, Map.WATER, 3f);
                    path = map.GetPath(sx, sy, ex, ey, Map.GROUND);
                }
            }
            if (keyboard.IsKeyDown(Keys.D3))
            {
                int x = state.X / 16;
                int y = state.Y / 16;
                if (x >= 0 && x < 50 && y >= 0 && y < 37)
                {
                    map.SetTile(x, y, Map.MOUNTAIN, 7f);
                    path = map.GetPath(sx, sy, ex, ey, Map.GROUND);
                }
            }

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin();
            map.Render(spriteBatch);
            spriteBatch.End();

            if (path.Count != 0)
            {
                Vector2 prev = new Vector2();
                bool first = true;
                foreach (int i in path)
                {
                 /*   if (first)
                    {
                        prev = new Vector2((i % 50) * 16f + 8f, (i / 50) * 16f + 8f);
                        first = false;
                    }
                    else
                    {
                        Vector2 pos = new Vector2((i % 50) * 16f + 8f, (i / 50) * 16f + 8f);
                        Util.Debug.Shape.DrawLineLocal(pos.X, pos.Y, prev.X, prev.Y, Color.Red);
                        prev = pos;
                    }*/
                    Util.Debug.Shape.DrawSolidCircleLocal((i % 50) * 16f + 8f, (i / 50) * 16f + 8f, 8, Color.Red);
                   
                }
            }


            Matrix m = Matrix.Identity;
            Util.Debug.Shape.RenderDebugData(ref m);

            base.Draw(gameTime);
        }
    }
}
